/**
 * 第三场景 ScreenThree
 */
class ScreenThree extends egret.DisplayObjectContainer {
    /** 加载背景 */
    private bg_png = Main.createBitmapByName("bg_3_png");
    /** 加载小刀 */
    private dor_png = Main.createBitmapByName("dor_png");
    /** 加载报警器 */
    private bibibi_png = Main.createBitmapByName("bibibi_png");
    /** 加载角色 */
    private role_png = Main.createBitmapByName("role_3_0_png");
    /** 创建提示点 */
    private tipPoint = Main.createTipPoint();
    /** 创建bibibi提示点 */
    private tipPoint1 = Main.createTipPoint();
    /** 角色原地站立动画 */
    private role_timer: egret.Timer;

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        Main.createScale(this.bg_png, this);
        Main.createScale(this.role_png, this);
        this.knifeSetting();
        this.bibibiSetting();

        this.tipPoint.x = 660;
        this.tipPoint.y = 347;

        this.tipPoint1.x = 829;
        this.tipPoint1.y = 350;

        // Main.dragGetPatn(this.tipPoint1, this)
        this.roleAnimation();
    }
    /** 道具: 小刀设置 */
    private knifeSetting() {
        const miva = this;
        const knife_png = Main.createBitmapByName("dor_png");
        miva.addChild(knife_png);
        Main.anchorCenter(knife_png);
        knife_png.x = 343;
        knife_png.y = 267;
        knife_png.scaleX = .6;
        knife_png.scaleY = .6;
        Main.itemAniamtion(knife_png);
        knife_png.touchEnabled = true;
        knife_png.once(egret.TouchEvent.TOUCH_TAP, knifeTouchHandler, miva);
        /** 小刀点击事件处理 */
        function knifeTouchHandler() {
            egret.Tween.removeTweens(knife_png);
            /** 创建提示点 */
            Main.createScale(miva.tipPoint, miva);
            knife_png.scaleX = 1;
            knife_png.scaleY = 1;
            knife_png.rotation = 90;
            miva.removeChild(knife_png);
            miva.dispatchEventWith("getItem", false, { item: knife_png, tipPoint: miva.tipPoint }, false);
        }
    }
    /** 道具: 报警器设置 */
    private bibibiSetting() {
        // const bibibi_png = this.bibibi_png;
        this.addChild(this.bibibi_png);
        Main.anchorCenter(this.bibibi_png);
        this.bibibi_png.x = 635;
        this.bibibi_png.y = 250;
        this.bibibi_png.scaleX = .6;
        this.bibibi_png.scaleY = .6;
    }
    private bibibiEvent() {
        const miva = this;
        miva.bibibi_png.touchEnabled = true;
        miva.bibibi_png.once(egret.TouchEvent.TOUCH_TAP, bibibiTouchHandler, miva);
        /** 报警器点击事件处理 */
        function bibibiTouchHandler() {
            /** 创建提示点 */
            Main.createScale(miva.tipPoint1, miva);
            miva.bibibi_png.scaleX = 1;
            miva.bibibi_png.scaleY = 1;
            miva.removeChild(miva.bibibi_png);
            miva.dispatchEventWith("getItem", false, { item: miva.bibibi_png, tipPoint: miva.tipPoint1 }, false);
        }
    }
    /** 道具栏执行函数 */
    private run_num = 0;
    public run() {
        this.run_num++;
        console.log("run执行", this.run_num);
        if (this.run_num == 1) {
            this.cutTheLine();
            this.removeChild(this.tipPoint);
            this.bibibiEvent();
            Main.itemAniamtion(this.bibibi_png);
        } else if (this.run_num == 2) {
            this.role_png.texture = RES.getRes(`role_3_5_png`);
            // this.removeChild(this.bibibi_png);
            // this.removeChild(this.tipPoint1);
            this.jcRun();
        }
    }
    /** 播放角色站立动画 */
    private roleAnimation() {
        let num = 0;
        this.role_timer = new egret.Timer(1000 / 2);
        this.role_timer.addEventListener(egret.TimerEvent.TIMER, () => {
            num++;
            if (num > 1) num = 0;
            this.role_png.texture = RES.getRes(`role_3_${num}_png`);
        }, this);
        this.role_timer.start();
    }
    /** 播放切断绳子动画 */
    private cutTheLine() {
        this.role_timer.stop();
        const timer = new egret.Timer(1000 / 3, 3);
        let num = 1;
        timer.addEventListener(egret.TimerEvent.TIMER, () => {
            num++;
            this.role_png.texture = RES.getRes(`role_3_${num}_png`);
        }, this);
        timer.start();
    }
    /** 警察来 */
    private jcRun() {
        /** 角色动画定时器 */
        const timer = new egret.Timer(1000 / 3, 4);
        const run_png = Main.createBitmapByName(`JC1_png`);
        this.addChild(run_png);

        run_png.scaleX = 1.1;
        run_png.scaleY = 1.1;

        run_png.y = run_png.y - 50;

        let num = 1;
        timer.addEventListener(egret.TimerEvent.TIMER, () => {
            num++;
            run_png.texture = RES.getRes(`JC${num}_png`);
        }, this);

        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, () => {
            run_png.x = run_png.x + 100;
            egret.Tween.get(this).wait(3000).call(() => {
                this.over();
            })
        }, this);

        timer.start();
    }
    /** 完成 */
    private over() {
        const miva = this;
        const over_png = Main.createBitmapByName("over_png");
        const over_light_1 = Main.createBitmapByName("over_light_1_png");
        const over_light_2 = Main.createBitmapByName("over_light_2_png");

        center(over_light_2);
        center(over_light_1);
        center(over_png);

        over_light_2.alpha = 0;

        over_png.touchEnabled = true;
        over_png.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            this.dispatchEvent(new egret.Event("next"));
        }, this);

        egret.Tween.get(over_light_1, { loop: true }).to({ alpha: 0 }, 500).wait(500).to({ alpha: 1 }, 500);
        egret.Tween.get(over_light_2, { loop: true }).to({ alpha: 1 }, 500).wait(500).to({ alpha: 0 }, 500);

        function center(item) {
            Main.anchorCenter(item);
            item.x = miva.stage.stageWidth * .5;
            item.y = miva.stage.stageHeight * .5;
            miva.addChild(item);
        }

    }
}